*****************************************
*Create full JSCReg sample
*****************************************
set more off
use "Network Data/JSCReg_hiding_04012004.dta", clear
gen year=2004
append using "Network Data/JSCReg_hiding_11102006.dta"
replace year=2006 if year==.
tab year
**===17,599 firms in 2004

*merge with privatization data
merge m:1 okpo using "Prepared Data/privatized_allJSCReg.dta"
keep if _merge==3
drop _merge

*merge with region and industry data
merge m:1 okpo year using "Prepared Data/region_ind_allJSCReg.dta"
keep if _merge==3
drop _merge
**===14,543 firms with industry data in 2004

*merge with IV data
rename obo region
merge m:1 region using "Prepared Data/IV_allJSCReg.dta"
keep if _merge==3
drop _merge

*merge with tfp level data
merge m:1 okpo using "Prepared Data/econ_allJSCReg.dta"
drop if _merge==2
drop _merge

tab year if tfp!=. & lemp!=. & privatized!=.
**===12,040 firms with industry data & economic vars in 2004

*keep only those firms that are present in 2004
set more off
gen time=1 if year==2004
replace time=2 if year==2006
bysort okpo:egen mtime=min(time)
keep if mtime==1

*generate DOFF and DFO
tsset okpo time
gen dfo=foreign_in_chain-l.foreign_in_chain
gen doff=offshore-l.offshore

bysort okpo: egen max_dfo=max(dfo)
bysort okpo: egen max_doff=max(doff)
drop dfo doff time mtime 
rename max_dfo dfo
rename max_doff doff
save "Regressions Data/allJSCReg0406.dta",replace

gen nown2004=1 if year==2004 & has_owners==0
bysort okpo: egen nown=max(nown2004)
drop if nown==1

save "Regressions Data/allJSCReg0406_nown.dta",replace
